# Maintainer: Mario Emmenlauer <memmenlauer@biodataanalysis.de>

_realname=matio
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.5.23
pkgrel=4
pkgdesc="matio is a C library for reading and writing MATLAB MAT files (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64' 'clang32')
url='https://sourceforge.net/projects/matio/'
license=('spdx:BSD-2-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-hdf5")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc" "${MINGW_PACKAGE_PREFIX}-autotools")
source=(https://github.com/tbeu/matio/releases/download/v${pkgver}/matio-${pkgver}.tar.gz)
sha256sums=('9f91eae661df46ea53c311a1b2dcff72051095b023c612d7cbfc09406c9f4d6e')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  # autoreconf to get updated libtool files for clang
  autoreconf -fiv
}

build() {
  [[ -d "${srcdir}/build-${MSYSTEM}" ]] && rm -rf "${srcdir}/build-${MSYSTEM}"
  cp -rf "${_realname}-${pkgver}" "build-${MSYSTEM}"
  cd "${srcdir}/build-${MSYSTEM}"

  chmod +x ./configure
  ./configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --enable-shared \
    --with-hdf5

  make ${MAKEFLAGS}
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"

  # it is not clear why the following workaround is needed to
  # execute tests, but after touching the testsuite.at, the
  # tests work, when otherwise they fail:
  touch ./test/testsuite.at

  make check
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  make DESTDIR="${pkgdir}" -j1 install

  mkdir -p "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/"
}
